<template>
  <!-- <a-card :bordered="false"> -->
    <!-- table区域-begin -->
    <div>
      <a-table
        size="small"
        row-key="id"
        :columns="columns"
        :data-source="dataSource"
        :pagination="false"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="action" slot-scope="text, record">
          <a v-if="record.taskType=== 1" @click="handleClick(record)">办理</a>
          <a-divider v-if="record.taskType=== 1" type="vertical" />
          <a v-if="record.taskType=== 1" @click="handleEntrustClick(record)">委托</a>
          <a-popconfirm v-if="record.taskType=== 0" title="确定拾取任务吗?" @confirm="() => handleClaimTask(record)">
            <a >拾取</a>
          </a-popconfirm>
        </template>
      </a-table>
      <!-- 任务办理弹窗 -->
      <task-modal ref="taskModal" :taskData="taskData" :title="taskTitle" @submit="completeTaskSubmit"></task-modal>
      <!-- 任务委托弹窗 -->
      <task-entrust-modal ref="taskEntrustModal" @ok="taskEntrust"></task-entrust-modal>
    </div>
  <!-- </a-card> -->
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
import { backTask, completeTask, entrustTask,claimTask } from '@/api/task'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import taskModal from '@/views/modules/task/modal/TaskModal'
import taskEntrustModal from '@/views/modules/task/modal/taskEntrustModal'
import STable from '@/components/table'
export default {
  name: 'MyTask',
  components: {
    JSelectUserByDep,
    JSelectMultiUser,
    STable,
    taskEntrustModal,
    taskModal
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '流程名称',
          align: 'center',
          dataIndex: 'modelName',
          ellipsis: true
        },
        {
          title: '流程编码',
          align: 'center',
          dataIndex: 'modelKey'
        },
        {
          title: '业务标题',
          align: 'center',
          dataIndex: 'instanceName',
          ellipsis: true
        },
        {
          title: '发起人',
          align: 'center',
          dataIndex: 'creator'
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '当前环节',
          align: 'center',
          dataIndex: 'name',
          ellipsis: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        // list: '/activiti/task/getMyTaskList'
        list: '/activiti/task/getMyAllTaskList'
      },
      queryParam: {
        type: '0,1'
      },
      backVisible: false,
      taskData: {},
      form: this.$form.createForm(this),
      taskTitle: '',
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      }
    }
  },
  methods: {
    handleClick(data) {
      this.taskData = data
      this.taskTitle = '【' + data.modelName + '】流程办理'
      this.$refs.taskModal.editModalVisible = true
    },
    handleBackCancel() {
      this.backVisible = false
      this.form.setFieldsValue({
        backComment: ''
      })
    },
    handleBackOk() { // 流程退回
      this.form.validateFields((err, values) => {
        if (!err) {
          backTask({
            comment: this.form.getFieldValue('backComment'),
            id: this.taskData.id,
            procInstId: this.taskData.processInstanceId,
            formDataId: this.taskData.formDataId
          }).then(res => {
            if (res.success) {
              this.$message.success('退回成功')
              this.loadData()
              this.backVisible = false
              this.form.setFieldsValue({
                backComment: ''
              })
            } else {
              this.$message.error(res.message)
            }
          }).catch((error) => {
            console.log(error)
          })
        }
      })
    },
    completeTaskSubmit(data, appendFormData) { // 流程审批
      completeTask({
        comment: data.reason,
        taskId: this.taskData.id,
        procInstId: this.taskData.processInstanceId,
        formDataId: this.taskData.formDataId,
        usernames: data.ccUserRealNames,
        flag: data.flag,
        appendFormId: this.taskData.appendForm && this.taskData.appendForm.id,
        formData: this.taskData.appendForm && JSON.stringify(appendFormData),
        appendFormDataId: data.appendFormDataId,
        nodeId: data.nodeId,
        files: data.files
      }).then(res => {
        if (res.success) {
          this.$message.success('流程办理成功')
          this.loadData()
          this.$refs.taskModal.editModalVisible = false
        } else {
          this.$message.error(res.message)
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    handleEntrustClick(data) { // 点击委托
      this.taskData = data
      this.$refs.taskEntrustModal.modalVisible = true
    },
    taskEntrust(data) {
      const that = this
      if (!data) {
        this.$message.error('请先选择委托人')
        return false
      }
      entrustTask({ taskId: this.taskData.id, userId: data }).then(function(res) {
        if (res.success) {
          that.$message.success('流程委托成功')
          that.loadData()
          that.$refs.taskEntrustModal.modalVisible = false
        } else {
          that.$message.error(res.message)
        }
      })
    },
    handleClaimTask(record) { // 拾取流程
      claimTask({
        taskId: record.id
      }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
